package com.itrui.comment.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itrui.comment.common.Code;
import com.itrui.comment.common.R;
import com.itrui.comment.common.Result;
import com.itrui.comment.pojo.Blacklist;
import com.itrui.comment.service.BlacklistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blacklist")
//拉黑，解黑，是否存在黑名单，黑名单列表
public class BlacklisController {
    @Autowired
    private BlacklistService blacklistService;

    @PostMapping
    public Result addBlackList(@RequestBody Blacklist blacklist) {

        boolean save = blacklistService.save(blacklist);
        return save ? new Result(Code.SAVE_OK, "成功拉黑") : new Result(Code.SAVE_ERR, "拉黑失败");

    }

    @DeleteMapping("/{id}/{target}")
    public Result deleteBlacklist(@PathVariable("id") Long userId, @PathVariable("target") Long target) {

        LambdaQueryWrapper<Blacklist> blacklistLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blacklistLambdaQueryWrapper.eq(Blacklist::getUserId, userId).eq(Blacklist::getTarget, userId);

        boolean remove = blacklistService.remove(blacklistLambdaQueryWrapper);
        return remove ? new Result(Code.DELETE_OK, "成功解除黑名单") : new Result(Code.DELETE_ERR, "解除黑名单失败");

    }

    @GetMapping("/is")
    public Boolean isBlack(@PathVariable("id") Long userId, @PathVariable("target") Long target) {
        LambdaQueryWrapper<Blacklist> blacklistLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blacklistLambdaQueryWrapper.eq(Blacklist::getUserId, userId).eq(Blacklist::getTarget, userId);

        Blacklist one = blacklistService.getOne(blacklistLambdaQueryWrapper);

        if (one != null) {
            return true;
        } else {
            return false;
        }
    }

    @GetMapping("/{id}")
    public Result BlackList(@PathVariable Long id) {
        LambdaQueryWrapper<Blacklist> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Blacklist::getUserId, id);
        List<Blacklist> list = blacklistService.list(queryWrapper);
        return new Result(Code.GET_OK, list, "获取黑名单列表成功");
    }
}
